﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace FisheryPlatform.Search.Core.Utils
{
    public static class AppSettings
    {
        // Methods
        public static bool Get(string key, bool defValue)
        {
            int i = ConfigurationManager.ConnectionStrings.Count;
            string theValue = ConfigurationManager.AppSettings[key];
            if (theValue == null)
            {
                return defValue;
            }
            return Convert.ToBoolean(theValue.Trim());
        }

        public static int Get(string key, int defValue)
        {
            int i = ConfigurationManager.ConnectionStrings.Count;
            string theValue = ConfigurationManager.AppSettings[key];
            if (theValue == null)
            {
                return defValue;
            }
            return Convert.ToInt32(theValue.Trim());
        }

        public static string Get(string key, string defValue)
        {
            int i = ConfigurationManager.ConnectionStrings.Count;
            string theValue = ConfigurationManager.AppSettings[key];
            if (theValue == null)
            {
                return defValue;
            }
            return theValue;
        }

        public static T Get<T>(string key, T defValue)
        {
            T ret;
            int i = ConfigurationManager.ConnectionStrings.Count;
            string theValue = ConfigurationManager.AppSettings[key];
            if (theValue == null)
            {
                return defValue;
            }
            Type type = typeof(T);
            if (type.IsEnum)
            {
                ret = (T)Enum.Parse(type, theValue);
            }
            else
            {
                ret = (T)type.InvokeMember("Parse", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { theValue });
            }
            return ret;
        }
    }


}
